/*-----------------------------
----$Source: Volumes://MATEOR/vais/modules/main/main.c$
----$Date: June 30, 2012$
-------------------------------
VAIS Copyright Matthew Caswell, 2012
-----------------------------*/
/*-- Estos son los incluyos de headers --*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<vais.h>
#include<lstring.h>
#include<lmath.h>
#include<lds_ts.h>
#include<sys/types.h>
#include<pthread.h>
#include<init.h>
#include<termios.h>
#include<braindo.h>

static struct termios old, new;

/*-- Estos son los prototipos de funciones despues la funcione primera --*/
void* hello (void* arg);

/*-- Estas son las funciones segundas --*/
void error (char* msg){
  printf ("\e[1;33m!-- ERROR -> \e[1;34m%s\e[0m\e[1;33m --!\e[0m\n", msg);
}

void debug (char* msg, char* func, char* page){
  if(AI_dbg == 1) printf ("\e[1;35m[ DEBUG: %s: %s: %s ]\e[0m\n", page, func, msg);
}

void debug_c (char c, char* func, char* page){
  if(AI_dbgc == 1) printf ("\e[1;32m[ DEBUG: %s: %s: %c ]\e[0m\n", page, func, c);
}

void debug_i (int i, char* func, char* page){
  if(AI_dbg == 1) printf ("\e[1;32m[ DEBUG: %s: %s: %i ]\e[0m\n", page, func, i);
}

/*-- Esto se inicia TERMIOS --*/
void initTermios (int echo){
  tcgetattr (0, &old);
  new = old;
  new.c_lflag &= ~ICANON;
  new.c_lflag &= echo ? ECHO : ~ECHO;
  tcsetattr (0, TCSANOW, &new);
}

void resetTermios (void){
  tcsetattr (0, TCSANOW, &old);
}

char getch_ (int echo){
  char ch;
  initTermios (echo);
  ch = getchar ();
  resetTermios ();
  return ch;
}

char getch (void) {
  return getch_ (0);
}

char getche (void){
  return getch_ (1);
}

/*-- Esta es la funcione primera --*/
int main (void){
  /* Actual VAIS empieza aqui */
  
  printf ("\e[1;33m[-- VAIS Copyright Matthew Caswell, 2012 --]\e[0m\n");
  
  // Recibimos la TimeStamp de construyo.
  
  timeStamp_t* VAIS_build_TS = malloc (sizeof (*VAIS_build_TS));
  h_TimeStamp (VAIS_build_TS, "build.TimeStamp");
  
  printf ("\e[1;33m[-- TimeStamp of VAIS build: DATE=%s TIME=%s COM=%s%s%s --]\e[0m\n",
    VAIS_build_TS->DS_datestamp[0],
    VAIS_build_TS->DS_timestamp[0],
    VAIS_build_TS->DS_lo[0],
    VAIS_build_TS->DS_m[0],
    VAIS_build_TS->DS_hi[0]);
    
  int n,i;
  pthread_t* threads;
  pthread_attr_t pthread_custom_attr;
  parm* t;
  
  n = 2;

  threads = (pthread_t*)malloc (n * sizeof (*threads));
  pthread_attr_init (&pthread_custom_attr);
  
  
  t = (parm*)malloc (sizeof (parm) * n);
  
  /* Empieza "thread" */
  t[0].id = 0;
  pthread_create (&threads[0], &pthread_custom_attr, hello, (void*)(t + 0));
  t[1].id = 1;
  pthread_create (&threads[1], &pthread_custom_attr, VAIS_init, (void*)"VAIS.memory/VAIS.init.DS");
  /* Syncronice el fin de todo "threads" */
  for(i = 0; i<n; i++){
    pthread_join (threads[i], null);
  }
  
  printf ("Press any key to end.\n");
  char meAmaBrianna = getch ();  // :D
  
  char laAmoBrianna[] = { '0', '0', '0', '0', '0', '0', ' ', ' ', meAmaBrianna};
  
  timeStamp_t* laAmoBrianna_TS = malloc (sizeof (*laAmoBrianna_TS));
  h_TimeStamp (laAmoBrianna_TS, "VAIS.memory/VAIS.laAmoBrianna.TimeStamp");
  
  printf (":D Last time you typed \"%s.\"\n", laAmoBrianna_TS->DS_hi[0]);
  
  VAIS_Memorize (laAmoBrianna, "DS_TS", "VAIS.memory/VAIS.laAmoBrianna.TimeStamp");
  
  free (t);
  printf ("\e[1;31m[-- COMPLETADO --]\e[0m\n");
  
  
  return (0);
}

void* hello (void* arg){
  parm* p = (parm*)arg;
  printf ("\e[1;36m{-- %d : This is VAIS/J30 --}\e[0m\n", p->id);
  printf ("\e[1;36m{-- %d : VAIS is a Variegated Artificial Intelligence System --}\e[0m\n", p->id);
  printf ("\e[1;36m{-- %d : VAIS is POSIX compliant --}\e[0m\n", p->id);
  printf ("\e[1;36m{-- %d : VAIS source code is VAICS complimentary --}\e[0m\n", p->id);
  printf ("\e[1;36m{-- %d : VAICS stands for Variegated Artificial Intelligence Coding Standard --}\e[0m\n", p->id);
  return (null);
}
